package cn.yqn.org.controller;


import cn.yqn.basic.exception.BusinessException;
import cn.yqn.basic.utils.ExcelUtil;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.org.domain.Shop;
import cn.yqn.org.domain.ShopAuditLog;
import cn.yqn.org.query.ShopQuery;
import cn.yqn.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
    * @Title: 店铺审核通过业务
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/26 21:27
    * @Parameters: [shopAuditLog]
    * @Return cn.yqn.basic.utils.JsonResult
    */
    @PostMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("审核失败!");
        }
    }

    /** 
    * @Title: 店铺审核驳回业务
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/26 21:36  
    * @Parameters: [shopAuditLog]
    * @Return cn.yqn.basic.utils.JsonResult
    */
    @PostMapping("/audit/reject")
    public JsonResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("审核失败!");
        }
    }

    /** 
    * @Title: 导出店铺信息业务
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/26 21:50  
    * @Parameters: [response]
    * @Return void
    */
    @GetMapping("/import/excel")
    public void exportExcel(HttpServletResponse response) {
        try {
            // 查询所有shop数据
            List<Shop> list = shopService.loadAll();
            ExcelUtil.exportExcel(list,null, "店铺数据", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
    * @Title: 导入店铺信息业务
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/27 1:04
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
            List<Shop> list = ExcelUtil.importExcel(file, 0, 1, Shop.class);
            // 保存到数据库表中
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query)
    {
        return shopService.pageQuery(query);
    }

    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return JsonResult.my();
        } catch (BusinessException e) {
            // 逻辑问题
            e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            // 代码问题
            return JsonResult.error().setMessage("系统异常,稍后重试");
        }
    }

    /**
    * @Title: 报表数据查询
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/27 12:53
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @GetMapping("/echarts")
    public Map<String, Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }
}
